﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Parichay.Domain;
using Parichay.Data.Helper;

namespace Parichay.Data.Repository
{
   public  class MemberRepository : Parichay.Domain.Interface.Data.IMemberRepository
    {
        public int[] getFriendIds(int userId)
        {
            System.Collections.IList lstIds = Data.Helper.NHibernateHelper.Find("select a.MemberDetails.Id , a.Friendid.Id from MemberFriends a where a.MemberDetails.Id=? or a.Friendid.Id=?", new object[] { userId, userId }, new NHibernate.Type.IType[] { NHibernate.NHibernateUtil.Int32, NHibernate.NHibernateUtil.Int32 }, false);

            if ((lstIds != null) && (lstIds.Count != 0))
            {
                List<int> lst = new List<int>();
                object[] pairIds = Data.Helper.PropertyInspector.ListToTypeArray(lstIds, typeof(Domain.Models.IntPair));
                foreach (Domain.Models.IntPair item in pairIds)
                {
                    if (item.Id1 == userId)
                    {
                        lst.Add(item.Id2);
                    }
                    else
                    {
                        lst.Add(item.Id1);
                    }
                }
                return ((int[])lst.ToArray<int>());
            }
            else { return null; }
        }


        public MemberDetails getUserById(int userId)
        {
            return (NHibernateHelper.UniqueResult<MemberDetails>(null, "Id", userId));
        }


        public void destroyAllUserData(string userId,string userEmail)
        {

            object obj7 = Data.Helper.NHibernateHelper.Delete("from MemberAlert a where a.PUser.Id=?", userId, NHibernate.NHibernateUtil.Int32);
            object obj8 = Data.Helper.NHibernateHelper.Delete("from MemberFriends a where a.MemberDetails.Id=? or a.Friendid.Id=?", new object[] { userId, userId }, new NHibernate.Type.IType[] { NHibernate.NHibernateUtil.Int32, NHibernate.NHibernateUtil.Int32 });
            //object obj2 = Data.Helper.NHibernateHelper.Delete("from MemberGroupmessages a where a.Sender.Id=? and a.Parent is null", userId, NHibernate.NHibernateUtil.Int32);
            //object obj2 = Data.Helper.NHibernateHelper.Delete("from MemberGroupmessages a where a.Sender.Id=?", userId, NHibernate.NHibernateUtil.Int32);
            object obj3 = Data.Helper.NHibernateHelper.Delete("from MemberGroupmembers a where a.MemberDetails.Id=?", userId, NHibernate.NHibernateUtil.Int32);
            //object obj4 = Data.Helper.NHibernateHelper.Delete("from MemberMessage a where a.Sender.Id=? or a.Recipient.Id=?", new object[] { userId, userId }, new NHibernate.Type.IType[] { NHibernate.NHibernateUtil.Int32, NHibernate.NHibernateUtil.Int32 });
            object obj5 = Data.Helper.NHibernateHelper.Delete("from MemberRequests a where a.Senderid.Id = ? or a.Recipientid.Id = ?", new object[] { userId, userId }, new NHibernate.Type.IType[] { NHibernate.NHibernateUtil.Int32, NHibernate.NHibernateUtil.Int32 });
            object obj6 = Data.Helper.NHibernateHelper.Delete("from MemberInvitations a where a.Senderid.Id = ? or a.Email = ?", new object[] { userId, userEmail }, new NHibernate.Type.IType[] { NHibernate.NHibernateUtil.Int32, NHibernate.NHibernateUtil.String });
            object obj11 = Data.Helper.NHibernateHelper.Delete("from MemberAbout m where m.Id=?", userId, NHibernate.NHibernateUtil.Int32);
            object obj12 = Data.Helper.NHibernateHelper.Delete("from MemberUploads m where m.Owner.Id=?", userId, NHibernate.NHibernateUtil.Int32);
            object obj17 = Data.Helper.NHibernateHelper.Delete("from MemberBlog m where m.Id=?", userId, NHibernate.NHibernateUtil.Int32);

            System.Collections.IList lstGrpChildMsg = Data.Helper.NHibernateHelper.Find("from MemberGroupmessages a where ((a.Sender.Id=? or a.Recipient.Id=?) and (a.Parent.Id is not null))", new object[] { userId, userId }, new NHibernate.Type.IType[] { NHibernate.NHibernateUtil.Int32, NHibernate.NHibernateUtil.Int32 }, false);

            foreach (Parichay.Domain.MemberGroupmessages itm in lstGrpChildMsg)
            {
                itm.Parent = null;
                itm.Recipient = null;
                Parichay.Data.Helper.NHibernateHelper.Update(itm);
                Parichay.Data.Helper.NHibernateHelper.Delete(itm);
            }

            object obj2 = Data.Helper.NHibernateHelper.Delete("from MemberGroupmessages a where a.Sender.Id=?", userId, NHibernate.NHibernateUtil.Int32);
            obj2 = Data.Helper.NHibernateHelper.Delete("from MemberGroupmessages a where a.Recipient.Id=?", userId, NHibernate.NHibernateUtil.Int32);


            System.Collections.IList lstChildMsg = Data.Helper.NHibernateHelper.Find("from MemberMessage a where ((a.Sender.Id=? or a.Recipient.Id=?) and (a.ParentId.Id is not null))", new object[] { userId, userId }, new NHibernate.Type.IType[] { NHibernate.NHibernateUtil.Int32, NHibernate.NHibernateUtil.Int32 }, false);
            foreach (Parichay.Domain.MemberMessage itm in lstChildMsg)
            {
                itm.ParentId = null;
                itm.Recipient = null;
                Parichay.Data.Helper.NHibernateHelper.Update(itm);
                Parichay.Data.Helper.NHibernateHelper.Delete(itm);
            }
            object obj16 = Data.Helper.NHibernateHelper.Delete("from MemberMessage a where a.Sender.Id=?", userId, NHibernate.NHibernateUtil.Int32);
            obj16 = Data.Helper.NHibernateHelper.Delete("from MemberMessage a where a.Recipient.Id=?", userId, NHibernate.NHibernateUtil.Int32);


            System.Collections.IList lstReplies = Data.Helper.NHibernateHelper.Find("from MemberReplies a where a.Sender.Id=? or a.Recipient.Id=?", new object[] { userId, userId }, new NHibernate.Type.IType[] { NHibernate.NHibernateUtil.Int32, NHibernate.NHibernateUtil.Int32 }, false);
            foreach (Parichay.Domain.MemberReplies itm in lstReplies)
            {
                itm.Parent.Replies.Remove(itm);
                Parichay.Data.Helper.NHibernateHelper.Delete(itm);
            }
            //object obj4 = Data.Helper.NHibernateHelper.Delete("from MemberReplies a where a.Sender.Id=?", userId, NHibernate.NHibernateUtil.Int32);
            //obj4 = Data.Helper.NHibernateHelper.Delete("from MemberReplies a where a.Recipient.Id=?", userId, NHibernate.NHibernateUtil.Int32);
            object obj4b = Data.Helper.NHibernateHelper.Delete("from MemberPosts a where a.Sender.Id=? or a.Recipient.Id=?", new object[] { userId, userId }, new NHibernate.Type.IType[] { NHibernate.NHibernateUtil.Int32, NHibernate.NHibernateUtil.Int32 });

            object obj14 = Data.Helper.NHibernateHelper.Delete("from MemberDetails a where a.Id=?", userId, NHibernate.NHibernateUtil.Int32);

        }
    }
}